<?php
	require '../include/library.inc.php';
	require '../include/admin_login.inc.php';
	require '../include/paging.inc.php';
	
	
	if (!isset($_POST['is_submitted'])) {
		$_POST['is_submitted'] = '';
	}
	
	
	if ($_POST['is_submitted']) {
			
			$error_message = '';
			if (!$_POST['coor'])  $error_message .= 'Не са въведени координати<br/>';
			if (!$_POST['label'])  $error_message .= 'Не са въведено описание/адрес<br/>';
			if(!$error_message) {
				
				$sql = "INSERT INTO  map (label,coor)
						VALUES ('".escape($_POST['label'])."','".escape($_POST['coor'])."')";
				
				query($sql);
				
				$_SESSION['message'] = 'Записът е добавен в базата данни.';
				redirect(HTML_BASE_ADMIN.'map.php');
			}
	
	
	
	}
	
	
	
	$sql = "SELECT
				id, label,coor
			FROM
				map
		   ";
			  
	
	
	if (!isset($_GET['sort'])) $_GET['sort'] = 1;
	if (!isset($_GET['sortBy'])) $_GET['sortBy'] = 1;
	
	$paging = new Paging($sql, $_GET['sortBy'], $_GET['sort'], array(1=>"Подредба"), 30);
	$result = $paging->query();
	
	head_admin("Карта");
?>
<?php if ($error_message) { ?>
			<div class="message red">
				<h3>Грешка!</h3>
				<?=$error_message?>
			</div>
<?php } ?>


<?php if ($_SESSION['message']) { ?>

	<div class="message green">
		<?=$_SESSION['message']?>
	</div>
<?php unset($_SESSION['message']); } ?>
	
	<h3>Карта (
  <?=$paging->count()?>
	) елемнта</h3>
    
  <form id="map_form"  action="<?=$_SERVER['PHP_SELF']?>" method="post" enctype="multipart/form-data">
            <input type="hidden" name="is_submitted" value="1" />
			<dl class="clearfix" >
            	<dt><label >Координати:</label></dt>
                <dd><input class="inputbox w500"   name="coor" type="text" value="<?=$_POST['coor']?>" ></dd>
                
                <dt><label >Описание</label></dt>
                <dd><textarea class="inputbox w500" name="label" rows="3" maxlength="255"><?=$_POST['label']?></textarea></dd>
                
                <a class="button" onclick="$('#map_form').submit(); return false;">Запиши</a>
                
             </dl>
  </form>
    
    
<table id="listing" width="100%" cellpadding="0" cellspacing="0">
  <thead>
    <tr>
      <td>Текст</td>
      <td>Координати</td>
      <td>Действия</td>
    </tr>
  </thead>
  <tbody>
    <?php
     
	 $i=1;
	 while ($row = mysql_fetch_object($result)) {				  
			?>
    <tr class="<?=(($i%2)?"none":"zebra")?>">
		<td> <?=sth($row->label)?></td>
        <td> <?=$row->coor?></td>
      	
      	<td class="icons">
        <a onClick="if (window.confirm('Сигурен ли си, че искаш да изтриеш <?=escape($row->label)?>?')) { document.location='map_del.php?id=<?=$row->id?>'; return false; }" class="delete" title="Изтрий">Изтрий</a>
        </td>
    </tr>
    <?php
              $i++;    
            } 
         ?>
  </tbody>
</table>
<div class="pagingwrap">
	<div class="paging">
    	<?=$paging->display()?>
	</div>
</div>

<?php
	foot_admin(); 
?>
